﻿Imports System.Web.UI
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.BusinessLayer.Views
Imports SISSAT.WebDevelopment.Views

Namespace [Shared].UserControls
    Public Class SubjectDetailControl
        Inherits UserControl
        Implements ISubjectDetailControlView

#Region "Fields"

        Private _controlMode As PageModes

#End Region

#Region "Properties"

        Property ErrorMessage As String
        Public Property UseControlModeFromUserInterface As Boolean

        Public Property ControlMode As PageModes
            Get
                Return _controlMode
            End Get
            Set(value As PageModes)
                If UseControlModeFromUserInterface Then
                    Exit Property
                End If

                _controlMode = value
                IsReadOnly = Not (_controlMode = PageModes.EditMode)
                CssClass = Convert.ToString(IIf(IsReadOnly, "textbox readonly", "textbox req"))

                InitializeComponents()
            End Set
        End Property

        Private Property Presenter As SubjectDetailControlPresenter
        Private Property CssClass As String
        Private Property IsReadOnly As Boolean

#End Region

#Region "ISubjectDetailView Members"

        Public Property Code As String Implements ISubjectDetailControlView.Code
            Get
                Return CodeTextBox.Text
            End Get
            Set(value As String)
                CodeTextBox.Text = value
            End Set
        End Property

        Public ReadOnly Property CurrentUserId As Integer Implements ISubjectDetailControlView.CurrentUserId
            Get
                Return SISSATPageBase.User.Employee.EmployeeType.EmployeeTypeId
            End Get
        End Property

        Public Property Description As String Implements ISubjectDetailControlView.Description
            Get
                Return DescriptionTextBox.Text
            End Get
            Set(value As String)
                DescriptionTextBox.Text = value
            End Set
        End Property

        Public Property Name As String Implements ISubjectDetailControlView.Name
            Get
                Return NameTextBox.Text
            End Get
            Set(value As String)
                NameTextBox.Text = value
            End Set
        End Property

        Public ReadOnly Property ScheduleId As Integer Implements ISubjectDetailControlView.ScheduleId
            Get
                Dim id As Integer = 0
                Dim scid As String = HttpContext.Current.Request.QueryString("scid")
                If Not String.IsNullOrEmpty(scid) AndAlso Integer.TryParse(scid, id) Then
                    ControlMode = PageModes.ViewMode
                End If

                Return id
            End Get
        End Property

        Public ReadOnly Property SubjectId As Integer Implements ISubjectDetailControlView.SubjectId
            Get
                Dim id As Integer = 0
                Dim sid As String = HttpContext.Current.Request.QueryString("sid")
                If Not String.IsNullOrEmpty(sid) AndAlso Integer.TryParse(sid, id) Then
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                ElseIf String.Compare(sid, "new", True) = 0 Then
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                Else
                    ControlMode = PageModes.ViewMode
                End If

                Return id
            End Get
        End Property

        Public Property Units As String Implements ISubjectDetailControlView.Units
            Get
                Return UnitsTextBox.Text
            End Get
            Set(value As String)
                UnitsTextBox.Text = value
            End Set
        End Property

        Public Event OnClear() Implements ISubjectDetailControlView.OnClear
        Public Event OnInitialization() Implements ISubjectDetailControlView.OnInitialization
        Public Event OnSubmit() Implements ISubjectDetailControlView.OnSubmit

#End Region

#Region "Methods"

        Private Sub InitializeComponents()
            ButtonPanel.Visible = Not IsReadOnly
            DescriptionPanel.Visible = Not IsReadOnly
            CodeTextBox.ReadOnly = IsReadOnly
            CodeTextBox.CssClass = CssClass
            DescriptionTextBox.ReadOnly = IsReadOnly
            NameTextBox.ReadOnly = IsReadOnly
            NameTextBox.CssClass = CssClass
            UnitsTextBox.ReadOnly = IsReadOnly
            UnitsTextBox.CssClass = CssClass
        End Sub

#End Region

#Region "Events"

        Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
            Presenter = New SubjectDetailControlPresenter(Me)
            RaiseEvent OnInitialization()
        End Sub

        Private Sub ClearButtonClick(sender As Object, e As EventArgs) Handles ClearButton.Click
            RaiseEvent OnClear()
        End Sub

        Private Sub SubmitButtonClick(sender As Object, e As EventArgs) Handles SubmitButton.Click
            RaiseEvent OnSubmit()
        End Sub

#End Region

    End Class
End Namespace

